Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 是否为修改
    id: "",
    // 银行卡号
    bankCardNumber: "",
    // 持卡人姓名
    realName: "",
    // 身份证号
    idCard: "",
    bankName: "",
    active: "",
    bankcardList: [
      {
        bankName: "寿光农村商业银行",
      },
    ],
  },

  onChange(e) {
    let active = e.currentTarget.dataset.index;
    if (this.data.active === active * 1) {
      this.setData({
        active: "",
      });
      return;
    }
    this.setData({
      active,
      bankName: this.data.bankcardList[active].bankName,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad(options) {
		// 接受传过来的参数
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on("acceptDataFromOpenerPage", (data) => {
      if (data.addbankcard) {
        const { id, bankCardNumber, idCard, realName, bankName } = data.addbankcard;
        this.setData({ id, bankCardNumber, idCard, realName, bankName });
      }
    });
  },

  toAddBankcard() {
    if (this.data.active === "") {
      return wx.showToast({
        title: "请选择一张银行卡",
        icon: "none",
      });
    }
    const { id, bankCardNumber, idCard, realName, bankName } = this.data;
    wx.setStorage({
      key: "addbankcard",
      data: {
        addbankcard: { id, bankCardNumber, idCard, realName, bankName },
      },
      success: function () {
        wx.navigateBack(); //返回上一个页面
      },
    });
  },
});
